<?php
namespace data\service;

use data\model\UserModel;

class UserService extends BaseService {

    private $userModel = null;

    public function __construct() {
        parent::__construct();
        $this->userModel = new UserModel();
    }

    public function getUserInfoByOpenid( $openid = "" , $fields = "*"){
        $res = $this->userModel->getInfo([ 'openid' => $openid ] , $fields);
        return empty($res) ? [] : $res;
    }

    public function getUserInfoByUid( $uid = "" , $fields = "*"){
        $res = $this->userModel->getInfo([ 'id' => $uid ] , $fields);
        return empty($res) ? [] : $res;
    }

    public function fieldInc($uid,$field,$step = 1){
        $res = $this->userModel->where('id='.$uid)->setInc($field,$step);
        return $res;
    }

    public function register( $openid ,$username,$avatar,$province,$city){
        $retVal = $this->userModel->save([
            'username' => $username,
            'openid' => $openid,
            'avatar' => $avatar,
            'ctime' => time(),
            'province' => $province,
            'city' => $city,
            'score' => 0,
            'lastlogin' => time(),
        ]);
        if( $retVal ){
            //如果注册成功，则存入redis
            return $this->userModel->id;
        }
        return 0;
    }

}
